using System.Net;
using System.Net.Sockets;

using Common;
using Common.NetWork;

using GRPCDemo.Protocol;

namespace XAliphant.Net.Client.Network
{
    public class NetClient : Singleton<NetClient>
    {
        private Socket? clientSocket;
        private bool isConnected = false;
        private IPEndPoint? remoteEndPoint;

        public bool IsConnected => isConnected;

        public void Connected(string ip, string port)
        {
            remoteEndPoint = new IPEndPoint(IPAddress.Parse(ip), int.Parse(port));
            if (isConnected)
            {
                CloseConnection();
            }
            DoConnected();
        }

        public void CloseConnection()
        {
            isConnected = false;
        }


        public void SendMessage(NetMessage message)
        {

        }

        private void DoConnected()
        {
            if (isConnected)
            {
                return;
            }

            if (clientSocket != null && clientSocket.Connected)
            {
                clientSocket.Blocking = false;
                isConnected = true;
            }
        }

        public void Update()
        {

        }
    }
}
